package com.ceair.mub2c.payment.payments.utils;

import java.io.IOException;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;




public class HttpClientUtil {
	
/** The Constant LOGGER. */
private static final Logger LOGGER = LoggerFactory
		.getLogger(HttpClientUtil.class);

	public static void main(String[] args) {
		LOGGER.info("response:" + httpPostRaw("http://localhost:8086/sales/accountVerifyPwd", "{\"sign\":\"1\",\"scCode\":\"test\",\"subScCode\":\"subtest\"}",null, "utf-8"));
	}
	/** 
	 * 发送 http post 请求，参数以原生字符串进行提交 
	 * @param url 
	 * @param encode 
	 * @return 
	 */  
	public static String httpPostRaw(String url,String stringJson,Map<String,String> headers, String encode){
		String response = "";
	    if(encode == null){  
	        encode = "utf-8";  
	    }  
	    //HttpClients.createDefault()等价于 HttpClientBuilder.create().build();   
	    CloseableHttpClient closeableHttpClient = HttpClients.createDefault();   
	    HttpPost httpost = new HttpPost(url);  
	    
	    //设置header
	    httpost.setHeader("Content-type", "application/json");    
	    if (headers != null && headers.size() > 0) {
	        for (Map.Entry<String, String> entry : headers.entrySet()) {
	            httpost.setHeader(entry.getKey(),entry.getValue());
	        }
	    }
	    //组织请求参数  
	    StringEntity stringEntity = new StringEntity(stringJson, encode);  
	    httpost.setEntity(stringEntity);  
	    String content = null;  
	    CloseableHttpResponse  httpResponse = null;  
	    try {  
	        //响应信息
	        httpResponse = closeableHttpClient.execute(httpost);  
	        HttpEntity entity = httpResponse.getEntity();  
	        content = EntityUtils.toString(entity, encode);  
	        response = content;
	    } catch (Exception e) {  
	        e.printStackTrace();  
	    }finally{  
	        try {  
	            httpResponse.close();  
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        }  
	    }  
	    try {  //关闭连接、释放资源  
	        closeableHttpClient.close();  
	    } catch (IOException e) {  
	        e.printStackTrace();  
	    }    
	    return response;  
	}  
	
}